package com.cqrt.aop;

import cn.hutool.core.util.StrUtil;
import com.cqrt.entity.PlanTemplate;
import com.cqrt.mapper.PlanTemplateMappper;
import com.hustcad.plm.rpm.model.dto.admin.TyppmProjectDTO;
import com.hustcad.plm.rpm.model.entity.plan.TyppmPlan;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Objects;

@Aspect
@Component
@Slf4j
public class InsertPlanByProjectAOP {

    @Resource
    private PlanTemplateMappper planTemplateMappper;

    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;

//    @Around(value = "execution(* com.hustcad.plm.rpm.service.impl.plan.TyppmPlanServiceImpl.insertPlanByProject(..))")
    public Object insertPlanByProject(ProceedingJoinPoint joinPoint) throws Throwable {
        TyppmProjectDTO project=(TyppmProjectDTO)joinPoint.getArgs()[0];
        TyppmPlan plan =(TyppmPlan) joinPoint.proceed();
        String planTemplateOID = project.getPlanTemplateOID();

        if (Objects.isNull(plan)){
            return plan;
        }
        if (StrUtil.isBlank(planTemplateOID)){
            return plan;
        }
        String s ="";
                s= planTemplateMappper.selectPlanOidByPJOid(planTemplateOID);
        if (StrUtil.isBlank(s)){
            return plan;
        }
        if (StrUtil.isBlank(plan.getOid())){
            return plan;
        }
        PlanTemplate planTemplate = new PlanTemplate();
        planTemplate.setOid(snowflakeIdComponent.getInstance().nextId().toString());
        planTemplate.setAoid(plan.getOid());
        planTemplate.setBoid(s);
        planTemplate.setModifystamp(LocalDateTime.now());
        planTemplate.setCreatestamp(LocalDateTime.now());
        planTemplate.setUpdatestamp(LocalDateTime.now());
        planTemplate.setUpdatecount(new BigInteger("0"));
        planTemplateMappper.insertPlanTemplate(planTemplate);
        return plan;
    }
}
